<template>
	<view class="container">
		<view class="register-box">
			<text class="title">注册</text>
			<input class="input" placeholder="请输入用户名" password v-model="username" />
			<input class="input" placeholder="请输入密码" password v-model="password" />
			<input class="input" placeholder="请确认密码" password v-model="confirmPassword" />
			<button class="btn" type="primary" @click="handleRegister">注册</button>
			<navigator url="/pages/login/login" class="navigateBack">已有账号？去登录</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				username: '',
				password: '',
				confirmPassword: ''
			}
		},
		methods: {
			handleRegister() {
				if (!this.username || !this.password || !this.confirmPassword) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}
				if (this.password !== this.confirmPassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: '注册中...'
				})
				console.log("点击注册，准备调用接口发送数据");
				// 把数据提交到服务器，所有接口的统一调用方式
				uniCloud.callFunction({
					// 调用的接口名字
					name: 'testAPI',
					// 上传的数据
					data: {
						'name': this.username,
						'password': this.password
					},
					success: (msg) => {
						console.log("addUser CallBack" + JSON.stringify(msg));
						setTimeout(() => {
							uni.hideLoading()
							uni.showToast({
								title: '注册成功'
							})
						}, 1000)
						uni.navigateBack();
					},fail: (res) => {
						console.warn("addUser CallBack fail"+res);
					}
				})
			}
		}
	}
</script>

<style scoped>
	.register-box {
		width: 85%;
		padding: 25rpx;
		background-color: #fff;
		border-radius: 10rpx;
		box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.1);
	}

	.input {
		width: 100%;
		height: 90rpx;
		border: 1rpx solid #ddd;
		border-radius: 5rpx;
		margin-bottom: 20rpx;
		padding-left: 20rpx;
		box-sizing: border-box;
	}

	.login-link {
		display: block;
		text-align: center;
		font-size: 30rpx;
		color: #07c160;
		margin-top: 20rpx;
	}

	.register-btn {
		width: 100%;
		height: 85rpx;
		background-color: #07c160;
		color: #fff;
		border-radius: 5rpx;
		margin-top: 20rpx;
	}

	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100vh;
		background-color: #f8f8f8;
	}

	.title {
		display: block;
		text-align: center;
		font-size: 40rpx;
		font-weight: bold;
		margin-bottom: 30rpx;
	}
</style>